package miningexplorerproject.controller;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

import javax.swing.JComboBox;

import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import miningexplorerproject.model.Attribut;
import miningexplorerproject.model.Individu;

public class ControllerWeka {
	private NaiveBayes nb;
	private Instances instancesModel;

	
	
public ControllerWeka(){

	DataSource modelData;
	try {
			modelData = new DataSource(getClass().getResource("/miningexplorerproject/resources/credit-g2.arff").getFile());
			instancesModel = modelData.getDataSet();
			instancesModel.setClassIndex(instancesModel.numAttributes() - 1);
			nb = new NaiveBayes();
			nb.buildClassifier(instancesModel);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
}
	
public int computeWithMethodBayes(Individu indiv) {
		
		int resultat = 0;
		
		Attribut checkingStatusSelected = indiv.getCheckingStatusSelected();
		Attribut creditHistorySelected = indiv.getCreditHistorySelected();
		Attribut foreginWorkerSelected = indiv.getForeignWorkerSelected();
		Attribut housing = indiv.getHousingSelected();
		Attribut otherPaymentPlanSelected = indiv.getOtherPaymentPlanSelected();
		Attribut purposeSelected = indiv.getPurposeSelected();
		Attribut savingStatusSelected = indiv.getSavingStatusSelected();
	
		try {
			String[] data = new String[7];
			data[0] = checkingStatusSelected.getAttributeName().toString();
			data[1] = creditHistorySelected.getAttributeName().toString();
			data[2] = purposeSelected.getAttributeName().toString(); 
			data[3] = savingStatusSelected.getAttributeName().toString(); 
			data[4] = otherPaymentPlanSelected.getAttributeName().toString(); 
			data[5] = foreginWorkerSelected.getAttributeName().toString(); 
			data[6] = housing.getAttributeName().toString(); 

			Instance instance = new Instance(15);
			instance.setDataset(instancesModel);
			instance.setValue(instancesModel.attribute("checking_status"), (String)data[0]);
			instance.setValue(instancesModel.attribute("credit_history"), (String)data[1]);
			instance.setValue(instancesModel.attribute("purpose"), (String)data[2]);
			instance.setValue(instancesModel.attribute("savings_status"), (String)data[3]);
			instance.setValue(instancesModel.attribute("foreign_worker"), (String)data[5]);
			instance.setValue(instancesModel.attribute("other_payment_plans"), (String)data[4]);	
			instance.setValue(instancesModel.attribute("housing"), (String)data[6]);
			
			double[] distribution  = nb.distributionForInstance(instance);
			System.out.println(distribution[0]+"---"+distribution[1]);		
			double good = distribution[0] * 100;

			return (int) good;
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultat;	
	}	
}
